# 40 3
# 20 10
# 20 20
# 20 5
list_1 = input().split(" ")
m = int(list_1[0])
n = int(list_1[1])
list_2 = []
for i in range(n):
    list_2.append(input().split(" "))
# print(list_2)
dp = [[0] * (m + 1) for i in range(n + 1)]
# print(dp)
for i in range(1, n + 1):
    for j in range(1, m + 1):
        if j >= int(list_2[i - 1][0]):  # 如果当前背包容量大于所放物品容量
            dp[i][j] = max(dp[i - 1][j], int(list_2[i - 1][1]) + dp[i - 1][j - int(list_2[i - 1][0])])
        else:
            dp[i][j] = dp[i - 1][j]
print(dp[n][m])
